
import { Message, MessageBox  } from 'element-ui';
//toast提示
export function toast(msg, type = 'success') {
    if (!msg) return;
    Message({
        message : msg,
        type
    })
}
// alert提示
export function alert(options) {
    return new Promise((resolve, reject) => {
        MessageBox({
            title : '提示',
            message : typeof options === 'string' ? options : options.msg,
            showClose : false,
            showCancelButton : Boolean(options?.showCancel) || false,
            closeOnClickModal : false,
            closeOnPressEscape : false,
        }).then(res => {
            resolve(res)
        }).catch(_ => {
            reject(2)
        })
    })
}

//为数组构造字段
export function createField (arr, options={}) {
    if (!options || !Object.keys(options).length) return arr;
    
    return arr.map(item => {
        for (let key in options) {
            item[key] = options[key]
        }
        return item;
    })
}

//表单验证
export function formCheck (formRef) {
    if (!formRef || !formRef.validate) return; 
    return new Promise((resolve, reject) => {
        formRef.validate(valid => {
            if (valid) {
                resolve(valid);
                return;
            }
            reject();
            return false;
        })
    })
}

//关闭popover
export function closePopover (popoverRef) {
    if (!popoverRef || !popoverRef.doClose) return;
    popoverRef.doClose();
}

//获取环境
export function getNodeEnv () {
    return process.env.NODE_ENV
}